#include "stdafx.h"
#include "SketchModel.h"

SketchModel::SketchModel(void)
{
	this->set2dRenderMode(CATMULL_ROM);
}

SketchModel::SketchModel(RenderAgentInterface *renderAgent,  int area_width, int area_height)
{
	this->renderAgent        = renderAgent;
	this->sketch_area_width  = area_width;
	this->sketch_area_height = area_height;
	this->rawHeight			 = 0;
	this->unitHeight         = 0;
	this->set2dRenderMode(CATMULL_ROM);
}

SketchModel::~SketchModel(void)
{
}

void SketchModel::addSketch(CSketch *sketch)
{
	this->sketches.push_back(sketch);
	this->sketchModelChanged();
}

void SketchModel::removeSketch()
{
	this->sketches.pop_back();
	this->sketchModelChanged();
}

void SketchModel::clearSketches()
{
	this->sketches.clear();
	this->sketchModelChanged();
}

void SketchModel::render2D()
{
	vector<CSketch *>::iterator it;
	for (it = sketches.begin(); it != sketches.end(); it++ )
		(*it)->display2D(this->renderMode);
}

void SketchModel::renderSketchInProgress(int numPixels, int pixels[][2])
{
	if (renderAgent != NULL)
		this->renderAgent->renderExtra2D(numPixels, pixels);
}

void SketchModel::render3D()
{
	if (renderAgent != NULL)
		this->renderAgent->render3D();
}

void SketchModel::initialize3D()
{
	if (renderAgent != NULL)
		this->renderAgent->initialize3D();
}

int SketchModel::getHeight()
{
	return rawHeight;
}

float SketchModel::getUnitHeight()
{
	return unitHeight;
}

void SketchModel::sketchModelChanged()
{
	updateHeight();
	this->renderAgent->generate3DModel(sketches, sketch_area_width, sketch_area_height);
}

void SketchModel::updateHeight()
{
	int max_height = -9999;
	int min_height =  9999;

	vector<CSketch*>::iterator it;

	for (it=sketches.begin(); it!=sketches.end(); it++)
	{
		if ( (*it)->max_height > max_height )
			max_height = (*it)->max_height;
		if ( (*it)->min_height < min_height )
			min_height = (*it)->min_height;
	}

	this->rawHeight = max_height - min_height;
	this->unitHeight = (float) this->rawHeight / this->sketch_area_height;
}
